package controleur;

import java.awt.Dimension;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import modele.JoystickEvent;
import vue.MainFrame;

public class Controleur {
	// Variable(s) statique(s)
	private static Controleur instance = null;
	private static ControleurCanard instanceCanard = null;
	
	// Variable(s) d'instance
	private int x;
	private int y;
	private int score;
	private int max;
	private float ratio;
	@SuppressWarnings("unused")
	private JoystickEvent event;
	private MainFrame fenetre;
	private ExecutorService controleurCanard;
	
	// Controleur(s)
	private Controleur() {
		this.max=0;
		this.ratio = 0;
		this.score = 0;
		this.fenetre = new MainFrame();
		Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		this.x = ((int)dimension.getWidth()/2)-24;
		this.y = ((int)dimension.getHeight()/2)-24;
	}
	
	// Accesseur(s)
	public int getX() {
		return this.x;
	}
	
	public int getY() {
		return this.y;
	}
	
	public MainFrame getFenetre() {
		return this.fenetre;
	}
	
	public int getScore() {
		return this.score;
	}
	
	public int getMax() {
		return this.max;
	}
	
	public float getRatio() {
		return this.ratio;
	}
	
	// Mutateur(s)
	public void setX(int x) {
		Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		this.x=x;
		if(this.x < 0) {
			this.x=0;
		}
		if(this.x > (int)dimension.getWidth()-46) {
			this.x=(int)dimension.getWidth()-46;
		}
	}
	
	public void setY(int y) {
		this.y=y;
		Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		if(this.y < 0) {
			this.y = 0;
		}
		if(this.y > (int)dimension.getHeight()-46) {
			this.y = (int)dimension.getHeight()-46;
		}
	}
	
	// Methode(s)
	public void lancerEvent() {
		this.event = new JoystickEvent();
	}
	
	public void lancerControleurCanard() {
		this.controleurCanard = Executors.newFixedThreadPool(1);
		Controleur.instanceCanard = new ControleurCanard();
		this.controleurCanard.execute(Controleur.instanceCanard);
	}
	
	public int verifTir(){
		boolean trouve = false;
		int i=0;
		while(i<Controleur.instanceCanard.getCanards().size()&&!trouve){
			if(this.x+24 >= Controleur.instanceCanard.getCanards().get(i).getX() && this.x+24 <= Controleur.instanceCanard.getCanards().get(i).getX()+96){
				if (this.y+24 >= Controleur.instanceCanard.getCanards().get(i).getY()  && this.y+24 <= Controleur.instanceCanard.getCanards().get(i).getY()+72){
					trouve=true;
				}
				else{
					i++;
				}
			}
			else{
				i++;
			}
		}
		if (i==Controleur.instanceCanard.getCanards().size()){
			return -1;
		}
		else{
			return i;
		}
	}
	
	public void incrementerScore() {
		this.score++;
	}
	
	public void incrementerMax() {
		float valeur;
		if(this.getMax()==0) {
			valeur = 0;
		} else {
			valeur = (this.getScore()/(float)this.getMax())*100;
		}
		this.ratio=valeur;
		this.max++;
	}
	
	// Methode(s) statique(s)
	public static Controleur getInstance() {
		return Controleur.instance;
	}
	
	public static ControleurCanard getInstanceCanard() {
		return Controleur.instanceCanard;
	}
	
	public static void createInstance() {
		if(Controleur.instance == null) {
			Controleur.instance=new Controleur();
		}
	}
}
